package com.people.core.log.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

/**
 * 控制打印输出工具类
 *
 * @author: liguannan
 * @date 2022/05/19
 */
@Slf4j
public class ConsoleUtil {

    /**
     * 如果信息长度大于ommitLength，则自动忽略中间的内容输出,目的减少无用日志量方便查看
     * @param value
     * @return
     */
    public static String shorterShowContent(Object value,Integer ommitLength) {
        String content = "";
        try {
            content = JSON.toJSONString(value, true);
        } catch (Exception e) {
            log.warn("JSON.toJSONString失败 value:" + value, e);
        }
        if (org.apache.commons.lang3.StringUtils.isNotBlank(content) && content.length() > ommitLength) {
            //前后超过ommitLength的部分用...代替
            content = String.format("%s...%s", content.substring(0, ommitLength), content.substring(content.length() - ommitLength));
        }

        return content;
    }

    public static void main(String[] args) {
        String s = shorterShowContent("", 200);
    }
}
